package com.woniu.service;

import com.woniu.dao.backpage.AdminUserDao;
import com.woniu.dao.homePage.UsersDao;
import com.woniu.entity.AdminUser;
import com.woniu.entity.Users;
import com.woniu.util.ResData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Sinks;

import java.util.ArrayList;

@Service
public class SecurityService implements UserDetailsService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private AdminUserDao adminUserDao;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        AdminUser userInfo = adminUserDao.getUserInfo(s);
        if (userInfo==null){
            throw new UsernameNotFoundException("用户不存在");
        }
        System.out.println(userInfo.getStatus());
        if (userInfo.getStatus()==1){
            throw new UsernameNotFoundException("该账户已禁用");
        }
        //String anths = String.join(",", userInfo.getAnths());
        //return new User(userInfo.getAccount(),passwordEncoder.encode(userInfo.getPassword()), AuthorityUtils.commaSeparatedStringToAuthorityList(anths));
        return new User(userInfo.getAccount(),passwordEncoder.encode(userInfo.getPassword()),new ArrayList<>());
    }
}
